package com.zjdiepu.www.manage.api.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.DecimalFormat;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.type.CodeType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.UUIDFactory;
import com.zjdiepu.www.ibiz.base.ISerialNumberService;
import com.zjdiepu.www.ibiz.system.IDpSalemanManagerService;
import com.zjdiepu.www.model.system.dto.DpBaseSaleman;

/**      
* 项目名称：dpqc-parent-manager-home   
* 类名称：SalemanManagerController   
* 类描述：   
* 创建人：zhangzj
* 创建时间：2017年4月12日 下午1:56:05   
* 修改人：zhangzj
* 修改时间：2017年4月12日 下午1:56:05   
* 修改备注：   
* @version V1.0     
*/ 
@RestController
@RequestMapping(value= "SalemanManagerController", produces="application/json;charset=UTF-8")
@Api(tags = {"业务员管理"}, produces="application/json;charset=UTF-8")
public class SalemanManagerController extends BaseController{
	
	@Resource
	private IDpSalemanManagerService salemanManagerService=null;
	@Resource
	private ISerialNumberService serialNumberService;

	
	/** 
	* @author zhangzj
	* @Title: getSalemanManagerPages 
	* 
	* @param @param page
	* @param @param rows
	* @param @param name
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ResponseBody
	@RequestMapping("/getSalemanManagerPages")
	public Map<String, Object> getSalemanManagerPages(Integer page,Integer rows,String name){
		Map<String, Object> maps = super.success();
		try 
		{
			Map<String, Object> aWhere = CollectionInitUtil.emptyMap();
			if(null != name && !("").equals(name))
			{
				aWhere.put("name", name);
			}
			PageInfo<DpBaseSaleman> pageinfo = salemanManagerService.queryByPage(page,rows,aWhere);
			
			maps.put("rows", pageinfo.getList());
			maps.put("total", pageinfo.getLastPage());
			maps.put("records", pageinfo.getTotal()); //获取返回的记录数  
		}
		catch (Exception e) 
		{
			return super.errorMassage(e, "出错了  Exception  || getSalemanManagerPages");
		}
		return maps;
	}
	/** 
	* @author zhangzj
	* @Title: insert 
	* 
	* @param @param saleman
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ApiOperation(value = "添加业务员", response = Boolean.class, httpMethod="POST", tags={"添加业务员"})
    @RequestMapping(value = "insert", method = RequestMethod.POST)
	public ApiResponse insert(@ModelAttribute DpBaseSaleman saleman){
		String salersmanCode=getSalersmanCode(saleman);
		saleman.setId(UUIDFactory.getUUID());
		saleman.setSalersmanCode(salersmanCode);
		salemanManagerService.insert(saleman);
		return ApiResponse.success(true);
	}
	/** 
	* @author zhangzj
	* @Title: getById 
	* 
	* @param @param id
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ResponseBody
	@RequestMapping("/getById")
	public Map<String,Object> getById(String id){
		Map<String, Object> maps = super.success();
		try 
		{	
			Map<String, Object> aWhere = CollectionInitUtil.emptyMap();
			if(null != id && !("").equals(id))
			{
				aWhere.put("id", id);
			}
			DpBaseSaleman dpBaseSaleman=salemanManagerService.getById(id);
			maps.put("result", dpBaseSaleman);
		}
		catch (Exception e) 
		{
			return super.errorMassage(e, "出错了  Exception  || getById");
		}
		return maps;
	}
	/** 
	* @author zhangzj
	* @Title: update 
	* 
	* @param @param dpSuggestionBox
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ResponseBody
	@RequestMapping("/update")
	public Map<String,Object> update(DpBaseSaleman saleman){
		Map<String, Object> maps = super.success();
		try 
		{	
			DpBaseSaleman bs=salemanManagerService.getById(saleman.getId());
			if(!bs.getSalersmanCode().equals(saleman.getSalersmanCode())){
				String salersmanCode=getSalersmanCode(saleman);
				saleman.setSalersmanCode(salersmanCode);
			}
			salemanManagerService.update(saleman);
			System.out.println("-------------update");
		}
		catch (Exception e) 
		{
			return super.errorMassage(e, "出错了  Exception  || udpate");
		}
		return maps;
	}
	/** 
	* @author zhangzj
	* @Title: delete 
	* 
	* @param @param id
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String,Object> delete(String id){
		Map<String, Object> maps = super.success();
		try 
		{	
			salemanManagerService.delete(id);
			System.out.println("-------------delete success!");
		}
		catch (Exception e) 
		{
			return super.errorMassage(e, "出错了  Exception  || delete");
		}
		return maps;
	}
	/** 
	* @author zhangzj
	* @Title: getSalersmanCode 
	* @Description: 生成业务员编码
	* @param @param saleman
	* @param @return    设定文件 
	* @return String    返回类型 
	* @version V1.0
	* @throws 
	*/
	private String getSalersmanCode(DpBaseSaleman saleman){
		int sNumber=serialNumberService.createSerialNumber(CodeType.SALERSMAN, saleman.getUnitCode(), false);
		return new DecimalFormat("0000").format(sNumber);
	}
}
